﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Serialization;

using SynapseGaming.LightingSystem.Core;
using SynapseGaming.LightingSystem.Editor;
using SynapseGaming.LightingSystem.Lights;
using SynapseGaming.LightingSystem.Rendering;

using Jitter;
using Jitter.Collision;
using Jitter.Collision.Shapes;
using Jitter.Dynamics;
using Jitter.Forces;
using Jitter.LinearMath;

namespace SunburnJitter
{
    /// <summary>
    /// Component for sphere shaped physical objects
    /// </summary>
    public class SphereRigidBodyComponent : RigidBodyComponent
    {
        private float _radius;
        public float Radius { get { return _radius; } set { _radius = value; RigidBody.Shape = new SphereShape(_radius); } }

        public SphereRigidBodyComponent(float radius)
        {
            _radius = radius;
        }

        public SphereRigidBodyComponent(float radius, Vector3 localPos)
        {
            _radius = radius;
            LocalPosition = localPos;
        }

        public SphereRigidBodyComponent(float radius, Vector3 localPos, Vector3 localOrientation)
        {
            _radius = radius;
            LocalPosition = localPos;
            LocalOrientation = localOrientation;
        }

        public override void OnInitialize()
        {
            RigidBody = new RigidBody(new SphereShape(_radius));
            base.OnInitialize();
            RigidBody.Position = startPosition.ToJVector();
            RigidBody.Orientation = Matrix.CreateFromQuaternion(startOrientation).ToJitterMatrix();
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationHelper.SerializeFieldOrEnum(ref _radius, info, "SphereRadius");

            base.GetObjectData(info, context);
        }

        public override void SetObjectData(SerializationInfo info, StreamingContext context)
        {
            SerializationHelper.DeserializeField(ref _radius, info, "SphereRadius", true);

            base.SetObjectData(info, context);
        } 
    }
}
